package org.app4j.passport.dao;

import java.util.List;

import javax.persistence.Query;

import org.app4j.passport.domain.User;
import org.app4j.runtime.persist.ManagedRepository;

/**
 * @author Chi
 */
public class UserDao extends ManagedRepository<User> {
  @SuppressWarnings("unchecked")
  public User findByUsername(String username) {
    Query query = this.getEntityManager().createQuery("from User where username=?1", User.class);
    query.setParameter(1, username);
    List<User> results = query.getResultList();
    if (results.isEmpty()) {
      return null;
    } else {
      return results.get(0);
    }
  }
}
